<?php

/*
 * Project_Name: Your node
 * Coder: Nguyễn Trần Minh Trung
 */

class Controller {

    public static $a_con = array('localhost', 'root', '', 'buy_website');
    public $a_ajax = array('', 'send-mail');
        public $a_normal = array('', 'mau-website', 'ten-mien','demo','du-an-cong-ty','dat-hang', 'hosting', 'tin-tuc', 'lien-he');
    protected $lan;

    function __construct($modules) {
        define('MOD', $modules);
        $this->check_con();
        $this->check_main_seg();
    }

    public static function get_model($model) {
        require_once ROOT . 'model/model.php';
        require ROOT . 'model/' . $model . '.php';
    }

    private function check_con() {
        require ROOT . 'library/connect.php';
        try {
            new Connect(self::$a_con, 'utf8');
        } catch (Exception $exc) {
            echo 'ssss';
        }
    }

    public static function get_lib($lib) {
        require ROOT . 'library/' . $lib . '.php';
    }

    public static function get_view($url, $data = array()) {
        global $lan;
        require ROOT . 'template/' . MOD . '/' . $url . '.php';
    }

    private function check_main_seg() {
        session_start();
        $url = new Url(MAIN_SEG);
        //check ajax request
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
            require 'controller/con_ajax.php';
            $page = $url->main_Segment($this->a_ajax, '404');
            if ($page != '404') {
                $con_ajax = new Con_Ajax();
                $con_ajax->{$page}();
            }
        } else {
            Controller::get_model('home');
            $tag = new home();
            $this->a_normal = array_merge($this->a_normal, $tag->get_menu_tag());
            require 'controller/con_normal.php';
            $page = $url->main_Segment($this->a_normal, 'trang-chu');
            $con_normal = new con_Normal();
            $con_normal->{$page}();
        }
    }

}
